// ignore_for_file: dangling_library_doc_comments, slash_for_doc_comments
/*** 
 * @Author: Zhong
 * @Date: 2025-03-07 16:12:34
 * @LastEditors: Zhong zhongxiaodong@eenet.com
 * @LastEditTime: 2025-08-22 16:45:09
 * @FilePath: /wan_android_flutter/lib/module_code/mine/pages/mine_page.dart
 * @Description: 
 */

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_connect/http/src/utils/utils.dart';
import 'package:wan_android_flutter/common_code/configs/app_config.dart';
import 'package:wan_android_flutter/common_code/utils/tools.dart';

import '../../login/pages/login_page.dart';

class MinePage extends StatelessWidget {
  static const String routeName = '/mine_tab';

  const MinePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            backgroundColor: const Color.fromARGB(255, 148, 201, 244),
            pinned: true,
            actions: [
              IconButton(
                onPressed: () {
                  // 退出登录提示
                  _showLogoutDialog();
                },
                icon: Icon(Icons.settings),
              ),
            ],
            expandedHeight: (kToolbarHeight + 60), //实际超过导航栏的高度只有60
            flexibleSpace: FlexibleSpaceBar(
              background: Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      const Color.fromARGB(255, 161, 205, 240),
                      const Color.fromARGB(255, 84, 170, 240),
                    ],
                  ),
                ),
                child: _buildTopContent(context),
              ),
            ),
          ),

          //支付宝会员
          SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsetsGeometry.only(left: 10, right: 10, top: 10),
              child: _buildSingleRowItem(icon: "tubiao-12.png", title: "支付宝会员"),
            ),
          ),

          //账单
          SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsetsGeometry.only(left: 10, right: 10, top: 10),
              child: _buildSingleRowItem(icon: "tubiao-11.png", title: "账单"),
            ),
          ),

          // 卡片列表
          SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsetsGeometry.only(left: 10, right: 10, top: 10),
              child: Card(
                margin: EdgeInsets.all(0),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    _buildSingleRowItem(icon: "tubiao-09.png", title: "总资产"),
                    _buildSingleRowItem(icon: "tubiao-08.png", title: "余额"),
                    _buildSingleRowItem(icon: "tubiao-07.png", title: "余额宝"),
                    _buildSingleRowItem(icon: "tubiao-06.png", title: "花呗"),
                  ],
                ),
              ),
            ),
          ),

          // 九宫格
          SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsetsGeometry.only(left: 10, right: 10, top: 10),
              child: Card(
                margin: EdgeInsets.all(0),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(""),
                    GridView.count(
                      padding: EdgeInsets.all(0),
                      crossAxisCount: 4,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      children: [
                        _buildGridViewItem(icon: "tubiao-01.png", title: "蚂蚁保"),
                        _buildGridViewItem(icon: "tubiao-02.png", title: "借呗"),
                        _buildGridViewItem(
                          icon: "tubiao-03.png",
                          title: "芝麻信用",
                        ),
                        _buildGridViewItem(icon: "tubiao-04.png", title: "银行卡"),
                        _buildGridViewItem(icon: "tubiao-05.png", title: "余利宝"),
                        _buildGridViewItem(icon: "tubiao-06.png", title: "网商贷"),
                        _buildGridViewItem(
                          icon: "tubiao-07.png",
                          title: "网商银行",
                        ),
                        _buildGridViewItem(
                          icon: "tubiao-08.png",
                          title: "数字人民币",
                        ),
                        _buildGridViewItem(icon: "tubiao-09.png", title: "信用卡"),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),

          // 我的民生保障
          SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsetsGeometry.only(
                left: 10,
                right: 10,
                top: 10,
                bottom: 10,
              ),
              child: Card(
                margin: EdgeInsets.all(0),
                color: Colors.white,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: EdgeInsetsGeometry.only(left: 10, top: 10),
                      child: Text(
                        "我的民生保障",
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        ),
                      ),
                    ),
                    GridView.count(
                      padding: EdgeInsets.all(0),
                      crossAxisCount: 4,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      children: [
                        _buildGridViewItem(icon: "tubiao-01.png", title: "蚂蚁保"),
                        _buildGridViewItem(icon: "tubiao-02.png", title: "借呗"),
                        _buildGridViewItem(
                          icon: "tubiao-03.png",
                          title: "芝麻信用",
                        ),
                        _buildGridViewItem(icon: "tubiao-04.png", title: "银行卡"),
                        _buildGridViewItem(icon: "tubiao-05.png", title: "余利宝"),
                        _buildGridViewItem(icon: "tubiao-06.png", title: "网商贷"),
                        _buildGridViewItem(
                          icon: "tubiao-07.png",
                          title: "网商银行",
                        ),
                        _buildGridViewItem(
                          icon: "tubiao-08.png",
                          title: "数字人民币",
                        ),
                        _buildGridViewItem(icon: "tubiao-09.png", title: "信用卡"),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // GridView-item
  Container _buildGridViewItem({required String icon, required String title}) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset(AppConfig.imgPath + icon, width: 40.w, height: 40.w),
          Text(
            title,
            style: TextStyle(
              fontSize: 14.sp,
              color: Colors.black,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  // 单行项
  Widget _buildSingleRowItem({required String icon, required String title}) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10),
      child: Container(
        padding: const EdgeInsets.only(left: 15, right: 15),
        color: Colors.white,
        width: double.infinity,
        height: 50.w,
        child: Row(
          children: [
            Image.asset(
              AppConfig.imgPath + icon,
              width: 40.w,
              height: 40.w,
              fit: BoxFit.cover,
            ),
            SizedBox(width: 10.w),
            Expanded(
              flex: 1,
              child: Text(
                title,
                style: TextStyle(
                  fontSize: 16.sp,
                  color: Colors.black,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            Image.asset(
              AppConfig.imgPath + 'youjiantou.png',
              width: 20.w,
              height: 20.w,
            ),
          ],
        ),
      ),
    );
  }

  // 构建顶部内容
  Container _buildTopContent(BuildContext context) {
    return Container(
      color: Colors.transparent,
      margin: EdgeInsets.only(top: Tools.getTotalHeight(context) - 16),
      child: Row(
        children: [
          //头像
          Padding(
            padding: EdgeInsetsGeometry.symmetric(horizontal: 10),
            child: ClipRRect(
              borderRadius: BorderRadiusGeometry.all(Radius.circular(6)),
              child: FadeInImage.assetNetwork(
                placeholder: "${AppConfig.imgPath}empty.png",
                image: "https://picsum.photos/60?image=100",
                width: 60.w,
                height: 60.w,
                fit: BoxFit.cover,
              ),
            ),
          ),
          //姓名、手机号
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsetsGeometry.only(top: 16, bottom: 2),
                child: Text(
                  "这里是名字",
                  style: TextStyle(
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w600,
                    color: Colors.black,
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(12),
                  color: const Color.fromARGB(255, 115, 185, 243),
                ),
                child: Text(
                  "150****2127",
                  style: TextStyle(fontSize: 12.sp, color: Colors.white),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // 退出登录提示框
  void _showLogoutDialog() {
    Get.dialog(
      AlertDialog(
        title: Text("提示"),
        content: Text("确定要退出登录吗？"),
        actions: [
          TextButton(
            onPressed: () {
              Get.back();
            },
            child: Text("取消"),
          ),
          TextButton(
            onPressed: () {
              //清除缓存数据
              Tools.clearCacheData();
              Get.offAllNamed(LoginPage.routeName);
            },
            child: Text("确定"),
          )
        ]
      )
    );
  }
}
